#coding=utf-8

import models
from rest_framework import serializers


class UserSerializer(serializers.Serializer):
    name = serializers.CharField(read_only=True)
    age = serializers.IntegerField(read_only=True)
    sex = serializers.ChoiceField(choices=models.SEX_SELECT)

    def create(self, validated_data):
        """
        Create and return a new `Snippet` instance, given the validated data.
        """
        return models.Name.objects.create(**validated_data)

    def update(self, instance, validated_data):
        """
        Update and return an existing `Snippet` instance, given the validated data.
        """
        instance.name = validated_data.get('name', instance.name)
        instance.age = validated_data.get('age', instance.age)
        instance.sex = validated_data.get('sex', instance.sex)
        instance.save()
        return instance

    class Meta:
        model = models.Name
        fields = ('name', 'sex', 'age')


